package de.gbeine.java.depview.ui.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;

import lombok.extern.slf4j.Slf4j;

import com.google.common.eventbus.EventBus;

import de.gbeine.java.depview.event.GraphLevelEvent;
import de.gbeine.java.depview.event.MouseModeEvent;
import de.gbeine.java.depview.event.ShowLabelsEvent;
import de.gbeine.java.depview.graph.ClassLevelStrategy;
import de.gbeine.java.depview.graph.FeatureLevelStrategy;
import de.gbeine.java.depview.graph.PackageLevelStrategy;
import de.gbeine.java.depview.mousemode.PickingMouseMode;
import de.gbeine.java.depview.mousemode.TransformingMouseMode;

/**
 *
 */
@Slf4j
public class OptionsMenu extends JMenu {

    /**
     *
     */
    private static final long serialVersionUID = -3556391360949054054L;

    /**
     * The event bus intance.
     */
    private final EventBus eventBus;

    /**
     * Create JMenu Options.
     * @param pEventBus The event bus intance.
     */
    public OptionsMenu(final EventBus pEventBus) {
        super("Options");
        this.initialize();
        this.eventBus = pEventBus;
    }

    /**
     * .
     */
    private void initialize() {
        final ButtonGroup mouseModeGroup = new ButtonGroup();
        final JRadioButtonMenuItem mntmTransforming = new JRadioButtonMenuItem("Graph Moving");
        mntmTransforming.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.this.eventBus.post(new MouseModeEvent(OptionsMenu.this, new TransformingMouseMode()));
            }
        });
        mntmTransforming.setSelected(true);
        mouseModeGroup.add(mntmTransforming);
        this.add(mntmTransforming);

        final JRadioButtonMenuItem mntmPicking = new JRadioButtonMenuItem("Element Moving");
        mntmPicking.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.this.eventBus.post(new MouseModeEvent(OptionsMenu.this, new PickingMouseMode()));
            }
        });
        mouseModeGroup.add(mntmPicking);
        this.add(mntmPicking);

        final JSeparator sptrMouse = new JSeparator();
        this.add(sptrMouse);

        final JCheckBoxMenuItem mntmLabels = new JCheckBoxMenuItem("Show Labels");
        mntmLabels.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.this.eventBus.post(new ShowLabelsEvent(OptionsMenu.this, mntmLabels.isSelected()));
            }
        });
        mntmLabels.setSelected(true);
        this.add(mntmLabels);

        final JSeparator sptrLabels = new JSeparator();
        this.add(sptrLabels);

        final ButtonGroup levelGroup = new ButtonGroup();
        final JRadioButtonMenuItem mntmPackage = new JRadioButtonMenuItem("Package Level");
        mntmPackage.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.log.debug("Do something");
                OptionsMenu.this.eventBus.post(new GraphLevelEvent(OptionsMenu.this, new PackageLevelStrategy()));
            }
        });
        mntmPackage.setSelected(true);
        levelGroup.add(mntmPackage);
        this.add(mntmPackage);

        final JRadioButtonMenuItem mntmClass = new JRadioButtonMenuItem("Class Level");
        mntmClass.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.log.debug("Do something");
                OptionsMenu.this.eventBus.post(new GraphLevelEvent(OptionsMenu.this, new ClassLevelStrategy()));
            }
        });
        levelGroup.add(mntmClass);
        this.add(mntmClass);

        final JRadioButtonMenuItem mntmMethod = new JRadioButtonMenuItem("Method Level");
        mntmMethod.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                OptionsMenu.log.debug("Do something");
                OptionsMenu.this.eventBus.post(new GraphLevelEvent(OptionsMenu.this, new FeatureLevelStrategy()));
            }
        });
        levelGroup.add(mntmMethod);
        this.add(mntmMethod);
    }
}
